package com.lzy.cloud.common.controller;

import com.lzy.cloud.common.dtos.ParameterDetailDto;
import com.lzy.cloud.common.request.ParameterDetailRequest;
import com.lzy.cloud.common.response.ParameterDetailResponse;
import com.lzy.cloud.common.service.ParameterDetailService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class ParameterDetailController {

    @Autowired
    private ParameterDetailService parameterDetailService;

    /**
     * 查询
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/select/id", method = RequestMethod.GET)
    public Result selectById(@RequestParam Long id) {
        Ensure.that(id).isNotNull("00000002");
        return new Result(DozerUtils.map(parameterDetailService.selectById(id), ParameterDetailResponse.class));
    }

    /**
     * 添加
     *
     * @param parameterDetailRequest
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result insert(@RequestBody ParameterDetailRequest parameterDetailRequest) {
        Ensure.that(parameterDetailRequest).isNotNull("00000002");
        Boolean flag = parameterDetailService.insert(DozerUtils.map(parameterDetailRequest, ParameterDetailDto.class));
        Ensure.that(flag).isTrue("00000003");
        return new Result(true);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Result delete(@RequestParam Long id) {
        Ensure.that(id).isNotNull("00000002");
        Boolean flag = parameterDetailService.delete(id);
        Ensure.that(flag).isTrue("00000005");
        return new Result(true);
    }

    /**
     * 修改
     *
     * @param parameterDetailRequest
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody ParameterDetailRequest parameterDetailRequest) {
        Ensure.that(parameterDetailRequest).isNotNull("00000002");
        Boolean flag = parameterDetailService.update(DozerUtils.map(parameterDetailRequest, ParameterDetailDto.class));
        Ensure.that(flag).isTrue("00000004");
        return new Result(true);
    }

    /**
     * 根据系统编码查询
     *
     * @param parameterDetailRequest
     * @return
     */
    @RequestMapping(value = "/query/code", method = RequestMethod.POST)
    public Result queryByCode(@RequestBody ParameterDetailRequest parameterDetailRequest) {
        List<ParameterDetailDto> list = parameterDetailService.queryByCode(DozerUtils.map(parameterDetailRequest, ParameterDetailDto.class));
        return new Result(DozerUtils.maps(list, ParameterDetailResponse.class));
    }
}
